package Controller;

import java.awt.event.KeyEvent;
import javax.swing.JTextField;

/**
 *
 * @author Esteban
 * Controller to perform the validation of data input througout any User Interface
 */
public class InputController {
    
    public InputController(){
        
    }
    
    public void checkSize(java.awt.event.KeyEvent evt, int maxSize){
        JTextField obj = (JTextField) evt.getSource();
        
        if (obj.getText().length() >= maxSize && obj.getSelectionEnd() - obj.getSelectionStart() <= 0) {
            evt.consume();
        }
    }
    
    public void numbersOlny(java.awt.event.KeyEvent evt){
        int id = evt.getID();
        if (id == KeyEvent.KEY_TYPED) {
            char c0 = evt.getKeyChar();
            try{
                if (!(Integer.parseInt(String.valueOf(c0)) > 0 || Integer.parseInt(String.valueOf(c0)) < 9)) {
                evt.consume();
                }
            }catch(NumberFormatException ex){
                evt.consume();
            }            
        }
    }
    
    public void lettersOnly(java.awt.event.KeyEvent evt){
        int id = evt.getID();
        if (id == KeyEvent.KEY_TYPED) {
            char c0 = evt.getKeyChar();
            try{
                if ((Integer.parseInt(String.valueOf(c0)) > 0 || Integer.parseInt(String.valueOf(c0)) < 9)) {
                evt.consume();
                }
            }catch(NumberFormatException ex){
                
            }            
        }
    }
}
    